﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.Modules;
using WP.Controls;
using WP.Controls.GridColumn;


using WP.RM.BusinessRule;
using WP.RM.BusinessLogical;
using WP.RM.DataAccess;

namespace WP.RM.Form
{
    public partial class frmRMCategorySetBOMCopy : BaseQryRpt 
    {
        private string mstrSourceCategroy = "";
        private string mstrSourceSet = "";

        private string mstrSourceCategroyID = "";
        private string mstrSourceSetID = "";

        private bool mblnSuccess = false  ;
        public frmRMCategorySetBOMCopy(string strSourceCategory, string strSourceSet)
        {
            InitializeComponent();
            LoadAllControl(new ArrayList(this.Controls));
            mstrSourceCategroyID = strSourceCategory;
            mstrSourceSetID = strSourceSet;

            mstrSourceCategroy = DataFunctions.GetFieldValue("rmCategory", "CategoryName", "CategoryID='" + strSourceCategory + "'");
            mstrSourceSet = DataFunctions.GetFieldValue("rmSet", "SetName", "SetID='" + strSourceSet + "'");
        }

        public void   CopyTo(ref bool blnSuccess, ref string strDestCategory, ref string strDestSet)
        {
            this.Text = "复制   类别 " + mstrSourceCategroy + "  系列 " + mstrSourceSet + "  到......";
            this.ShowDialog(); 
            strDestCategory = txtCategoryID.Text;
            strDestSet = txtSetID.Text;
            blnSuccess = mblnSuccess;
        }

        private bool VaildCheck( ref string strErrMsg )
        {
            if (txtCategory.Text == mstrSourceCategroy && txtSet.Text == mstrSourceSet)
            {
                strErrMsg = "不能复制，目标和源相同。";
                return false;
            }
            if (txtCategory.Text == "")
            {
                strErrMsg = "类别不能为空。";
                return false;
            }
            if (txtSet.Text == "")
            {
                strErrMsg = "系列不能为空";
                return false;
            }
            return true;
        }

        protected override void ubtLookUp_Click(object sender, EventArgs e)
        {
            string strFindValue = string.Empty;
            string strFieldName = string.Empty;
            string strSql = string.Empty;
            string strCaption = string.Empty;
            Control con = sender as Control;
            if (con == null) return;
            Control ocon = null;
            switch (con.Name)
            {
                case "ubtCategory":                
                    strFindValue = txtCategory.Text ;
                    strFieldName = "CategoryName";
                    strSql = BaseFunctions.GetLookUpSQL("rmCategory");
                    ocon = txtCategory ;
                    break;
              
                case "ubtSet"  :
                    strFindValue = txtSet.Text ;
                    strFieldName = "SetName";
                    strSql = BaseFunctions.GetLookUpSQL("rmSet");
                    ocon = txtSet ;
                    break;
            }
            if (mblnLookUpSortByDescription) strSql = DataFunctions.GetSqlByDescriptionSort(strSql);
            mblnLookUpSortByDescription = false;

            frmLookUp frmlookup = new frmLookUp(strSql, strCaption, strFindValue, strFieldName);
            frmlookup.ShowDialog();
            if (frmlookup.HaveReturnValue)
            {
                ocon.Text = frmlookup.GetReturnValue(strFieldName);
                ocon.Focus();
                if (con.Name == ubtCategory.Name)
                {
                    txtCategoryID.Text = frmlookup.GetReturnValue("CategoryID"); 
                }
                else if (con.Name == ubtSet.Name)
                {
                    txtSetID.Text = frmlookup.GetReturnValue("SetID");   
                }
            }
            frmlookup.Close();

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            mblnSuccess = false;
            this.Hide();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string strErrMsg ="";
            if (!VaildCheck(ref strErrMsg))
            {
                MsgBoxEx.Show(strErrMsg);
                return;
            }
            mblnSuccess = true;
            this.Hide();
        }
    }
}
